﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:ReportTemplate
	/// </summary>
	public partial class ReportTemplate
	{
		public ReportTemplate()
		{}
		#region  Method


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string R_Id,string TC_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from ReportTemplate");
			strSql.Append(" where R_Id='"+R_Id+"' and TC_Id='"+TC_Id+"' ");
			return DbHelperSQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Maticsoft.Model.ReportTemplate model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.R_Id != null)
			{
				strSql1.Append("R_Id,");
				strSql2.Append("'"+model.R_Id+"',");
			}
			if (model.TC_Id != null)
			{
				strSql1.Append("TC_Id,");
				strSql2.Append("'"+model.TC_Id+"',");
			}
			if (model.R_Task != null)
			{
				strSql1.Append("R_Task,");
				strSql2.Append("'"+model.R_Task+"',");
			}
			if (model.R_Title != null)
			{
				strSql1.Append("R_Title,");
				strSql2.Append("'"+model.R_Title+"',");
			}
			if (model.R_Formerly != null)
			{
				strSql1.Append("R_Formerly,");
				strSql2.Append("'"+model.R_Formerly+"',");
			}
			if (model.R_Path != null)
			{
				strSql1.Append("R_Path,");
				strSql2.Append("'"+model.R_Path+"',");
			}
			if (model.R_Status != null)
			{
				strSql1.Append("R_Status,");
				strSql2.Append("'"+model.R_Status+"',");
			}
			if (model.R_STime != null)
			{
				strSql1.Append("R_STime,");
				strSql2.Append("'"+model.R_STime+"',");
			}
			if (model.R_ETime != null)
			{
				strSql1.Append("R_ETime,");
				strSql2.Append("'"+model.R_ETime+"',");
			}
			strSql.Append("insert into ReportTemplate(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.ReportTemplate model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update ReportTemplate set ");
			if (model.R_Task != null)
			{
				strSql.Append("R_Task='"+model.R_Task+"',");
			}
			else
			{
				strSql.Append("R_Task= null ,");
			}
			if (model.R_Title != null)
			{
				strSql.Append("R_Title='"+model.R_Title+"',");
			}
			if (model.R_Formerly != null)
			{
				strSql.Append("R_Formerly='"+model.R_Formerly+"',");
			}
			else
			{
				strSql.Append("R_Formerly= null ,");
			}
			if (model.R_Path != null)
			{
				strSql.Append("R_Path='"+model.R_Path+"',");
			}
			if (model.R_Status != null)
			{
				strSql.Append("R_Status='"+model.R_Status+"',");
			}
			else
			{
				strSql.Append("R_Status= null ,");
			}
			if (model.R_STime != null)
			{
				strSql.Append("R_STime='"+model.R_STime+"',");
			}
			else
			{
				strSql.Append("R_STime= null ,");
			}
			if (model.R_ETime != null)
			{
				strSql.Append("R_ETime='"+model.R_ETime+"',");
			}
			else
			{
				strSql.Append("R_ETime= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where R_Id='"+ model.R_Id+"' and TC_Id='"+ model.TC_Id+"' ");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string R_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ReportTemplate ");
			strSql.Append(" where R_Id='"+R_Id+"' " );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.ReportTemplate GetModel(string R_Id)
		{
			/*StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" R_Id,TC_Id,R_Task,R_Title,R_Formerly,R_Path,R_Status,R_STime,R_ETime ");
			strSql.Append(" from ReportTemplate ");
			strSql.Append(" where R_Id='"+R_Id+"' and TC_Id='"+TC_Id+"' " );
			Maticsoft.Model.ReportTemplate model=new Maticsoft.Model.ReportTemplate();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}*/

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" R_Id,TC_Id,R_Task,R_Title,R_Formerly,R_Path,R_Status,R_STime,R_ETime ");
            strSql.Append(" from ReportTemplate ");
            strSql.Append(" where R_Id='" + R_Id+"' ");
            Maticsoft.Model.ReportTemplate model = new Maticsoft.Model.ReportTemplate();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }

		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.ReportTemplate DataRowToModel(DataRow row)
		{
			Maticsoft.Model.ReportTemplate model=new Maticsoft.Model.ReportTemplate();
			if (row != null)
			{
				if(row["R_Id"]!=null)
				{
					model.R_Id=row["R_Id"].ToString();
				}
				if(row["TC_Id"]!=null)
				{
					model.TC_Id=row["TC_Id"].ToString();
				}
				if(row["R_Task"]!=null)
				{
					model.R_Task=row["R_Task"].ToString();
				}
				if(row["R_Title"]!=null)
				{
					model.R_Title=row["R_Title"].ToString();
				}
				if(row["R_Formerly"]!=null)
				{
					model.R_Formerly=row["R_Formerly"].ToString();
				}
				if(row["R_Path"]!=null)
				{
					model.R_Path=row["R_Path"].ToString();
				}
				if(row["R_Status"]!=null)
				{
					model.R_Status=row["R_Status"].ToString();
				}
				if(row["R_STime"]!=null)
				{
					model.R_STime=row["R_STime"].ToString();
				}
				if(row["R_ETime"]!=null)
				{
					model.R_ETime=row["R_ETime"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
        /// 


        public DataSet GetTeachingClassCourseName(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TC_Name,TeachingClass.TC_Id");
            strSql.Append(" FROM TeachingClass");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        
        
        }

        public DataSet GetTemplateInform(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select R_Id,ReportTemplate.TC_Id,R_Task,R_Title,R_Formerly,R_Path,R_Status,R_ETime,TC_Name,Teacher.T_Name,TC_Date_Day,TC_Date_Time");
            strSql.Append(" FROM ReportTemplate,TeachingClass,Teacher,SelectCourse");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select R_Id,ReportTemplate.TC_Id,R_Task,R_Title,R_Formerly,R_Path,R_Status,R_STime,R_ETime ");
			strSql.Append(" FROM ReportTemplate,TeachingClass,Teacher");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}


        public DataSet GetAllInformation()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            strSql.Append(" R_Id,TC_Id,R_Task,R_Title,R_Formerly,R_Path,R_Status,R_STime,R_ETime ");
            strSql.Append(" FROM ReportTemplate ");
            return DbHelperSQL.Query(strSql.ToString());
        }

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" R_Id,TC_Id,R_Task,R_Title,R_Formerly,R_Path,R_Status,R_STime,R_ETime ");
			strSql.Append(" FROM ReportTemplate ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM ReportTemplate ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.TC_Id desc");
			}
			strSql.Append(")AS Row, T.*  from ReportTemplate T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		#endregion  MethodEx
	}
}

